home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gimp.idb / usr / freeware / share / gimp / scripts / asc2img.scm.z / asc2img.scm
Encoding:
GIMP Script-Fu Script  |  1999-07-21  |  6.8 KB  |  271 lines

  1. ; Chris Gutteridge / ECS Dept. University of Southampton, England
  2. ; "ASCII 2 Image" script for the Gimp.
  3. ;
  4. ; 8th April 1998
  5. ; Takes a filename of an ASCII file and converts it to a gimp image.
  6. ; Does sensible things to preserve indents (gimp-text strips them)
  7. ;
  8. ; cjg@ecs.soton.ac.uk
  9.  
  10. ; This program is free software; you can redistribute it and/or modify
  11. ; it under the terms of the GNU General Public License as published by
  12. ; the Free Software Foundation; either version 2 of the License, or
  13. ; (at your option) any later version.
  14. ; This program is distributed in the hope that it will be useful,
  15. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ; GNU General Public License for more details.
  18. ; You should have received a copy of the GNU General Public License
  19. ; along with this program; if not, write to the Free Software
  20. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ; Define the function:
  23.  
  24. (define (script-fu-asc-2-img inFile 
  25.                  inFont 
  26.                  inFontSize 
  27.                  inTextColor 
  28.                              inTrans
  29.                              inBackColor
  30.                  inBufferAmount
  31.                              inFlatten)
  32.     
  33.   (set! theImage (car (gimp-image-new 10 10 RGB) ) )
  34.   
  35.     
  36.  
  37.  
  38.   (set! theLayer (car (gimp-layer-new theImage 
  39.                       10 
  40.                       10 
  41.                       RGBA_IMAGE
  42.                       "layer 1"
  43.                       100 
  44.                       NORMAL) ) )
  45.  
  46.   (gimp-palette-set-background inBackColor)
  47.   (gimp-layer-set-name theLayer "Background")
  48.   (gimp-image-add-layer theImage theLayer 0)
  49.  
  50. (script-fu-asc-2-img-layer theImage theLayer inFile inFont inFontSize 
  51.                  inTextColor inBufferAmount inFlatten)
  52.  
  53.     (set! theBuffer (* inFontSize (/ inBufferAmount 100) ) )
  54.     (set! theImageWidth (+ theImageWidth theBuffer theBuffer ))
  55.     (set! theImageHeight (+ theImageHeight theBuffer theBuffer ))
  56.  
  57.         (gimp-image-resize theImage 
  58.                theImageWidth 
  59.                theImageHeight 
  60.                theBuffer 
  61.                theBuffer)
  62.         (gimp-layer-resize theLayer 
  63.                theImageWidth 
  64.                theImageHeight 
  65.                theBuffer 
  66.                theBuffer)
  67.       (gimp-selection-all theImage)
  68.         (if (= inTrans TRUE) 
  69.           (gimp-edit-clear theImage theLayer)
  70.           (gimp-edit-fill theImage theLayer)
  71.         )
  72.       (gimp-selection-none theImage)
  73.  
  74.         (if (= inFlatten TRUE) 
  75.         (gimp-image-merge-visible-layers theImage 0)
  76.             ()
  77.         )
  78.  
  79.  
  80.     (gimp-display-new theImage)
  81.     
  82.     (gimp-image-clean-all theImage)
  83.     
  84.     (gimp-displays-flush)
  85.     (cons theImage  ()   )
  86. )
  87.  
  88. (define (script-fu-asc-2-img-layer inImage
  89.                    inLayer
  90.                 inFile 
  91.                        inFont 
  92.                        inFontSize 
  93.                  inTextColor 
  94.                              inFlatten)
  95.     
  96.   (set! theImage inImage)
  97.   (set! theLayer inLayer)
  98.   (set! theFile (fopen inFile))
  99.  
  100.   (set! otherLayers (cadr (gimp-image-get-layers theImage)))
  101.   (set! nLayers (car (gimp-image-get-layers theImage)))
  102.   (set! n nLayers)
  103.   (if (= inFlatten TRUE)
  104.       (while (> n 0) (set! n (- n 1)) (gimp-layer-set-visible (aref otherLayers n) FALSE) )
  105.       ()
  106.   )
  107.  
  108.  
  109.   (gimp-palette-set-foreground inTextColor)
  110.   (gimp-selection-none theImage)
  111.   (set! theData ())
  112.   (set! theIndentList ())
  113.   (set! theChar "X")
  114.   (while (not (equal? () theChar))
  115.      (set! allspaces TRUE)
  116.      (set! theIndent 0)
  117.      (set! theLine "")
  118.      (while     (begin     (set! theChar (fread 1 theFile))
  119.                 (and     (not (equal? "\n" theChar))
  120.                     (not (equal? () theChar))
  121.                     )
  122.                 )
  123.             (cond     (    (equal? theChar "\t") 
  124.                     (set! theChar "        ") 
  125.                     (if (= allspaces TRUE) 
  126.                         (set! theIndent (+ theIndent 8)) 
  127.                         ())                
  128.                 )
  129.                 (    (equal? theChar " ") 
  130.                     (if (= allspaces TRUE) 
  131.                         (set! theIndent (+ theIndent 1)) 
  132.                         ())
  133.                 )
  134.                 (TRUE (set! allspaces FALSE))
  135.             )
  136.             (set! theLine (string-append theLine theChar))
  137.         )
  138.         (if     (= allspaces TRUE)
  139.             (set! theLine "")
  140.             ()
  141.         )
  142.         (if     (and     (equal? () theChar)
  143.                 (equal? "" theLine)
  144.             )
  145.             ()
  146.             (begin     (set! theData (cons theLine theData))
  147.                 (set! theIndentList 
  148.                       (cons theIndent theIndentList))
  149.                 )
  150.             )
  151.         )
  152.  
  153.   (set! theText (car (gimp-text theImage 
  154.                 -1 
  155.                 0 
  156.                 0 
  157.                 "X"
  158.                 0 
  159.                 TRUE
  160.                 inFontSize
  161.                 PIXELS 
  162.                 "*" 
  163.                 inFont
  164.                 "*" 
  165.                 "*"
  166.                 "*" 
  167.                 "*")))
  168.      (set! theCharWidth (car (gimp-drawable-width  theText) ))
  169.     (gimp-edit-cut theImage theText)
  170.  
  171.     (set! theImageHeight 0)
  172.     (set! theImageWidth 0)
  173.     (cjg-add-text (reverse theData) 
  174.               (reverse theIndentList) 
  175.               inFont 
  176.               inFontSize)
  177.       (if (= inFlatten TRUE)
  178.         (gimp-image-merge-visible-layers theImage 0)
  179.               ()
  180.       )
  181.       (set! n nLayers)
  182.       (if (= inFlatten TRUE)
  183.               (while (> n 0) (set! n (- n 1)) (gimp-layer-set-visible (aref otherLayers n) TRUE) )
  184.               ()
  185.       )
  186.     (gimp-displays-flush)
  187. )
  188.  
  189. (define (cjg-add-text inData inIndentList inFont inFontSize) 
  190.   (if     (equal? () inData) 
  191.     ()
  192.     (let     ((theLine (car inData)) (theIndent (car inIndentList)))
  193.       (if     (equal? "" theLine)
  194.         ()
  195.         (begin 
  196.           (set! theText (car (gimp-text theImage 
  197.                         -1 
  198.                         0 
  199.                         0 
  200.                         (string-append " " theLine) 
  201.                         0
  202.                         TRUE 
  203.                         inFontSize
  204.                         PIXELS
  205.                         "*" 
  206.                         inFont 
  207.                         "*" 
  208.                         "*" 
  209.                         "*"
  210.                         "*")))
  211.           (set! theLineHeight (car (gimp-drawable-height  theText) ) )
  212.           (gimp-layer-set-offsets theText 
  213.                       (* theCharWidth theIndent)  
  214.                       (+ theImageHeight 
  215.                          (- inFontSize theLineHeight)))
  216.           (set! theImageWidth (max 
  217.                        (+ (car (gimp-drawable-width  theText) )
  218.                       (* theCharWidth theIndent))
  219.                        theImageWidth ))
  220.           (if (= (car (gimp-layer-is-floating-sel theText)) TRUE)
  221.                       (gimp-floating-sel-anchor theText)
  222.               ()
  223.                   )
  224.                   (gimp-layer-set-name theText theLine)
  225.           )
  226.         )
  227.       (set! theImageHeight 
  228.         (+ theImageHeight inFontSize)) 
  229.       (cjg-add-text (cdr inData) (cdr inIndentList) inFont inFontSize)))
  230. )
  231.  
  232. ; Register the function with the GIMP:
  233.  
  234. (script-fu-register
  235.  "script-fu-asc-2-img"
  236.  "<Toolbox>/Xtns/Script-Fu/Utils/ASCII 2 Image"
  237.  "foo"
  238.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  239.  "8th April 1998"
  240.  "Chris Gutteridge / ECS @ University of Southampton, England"
  241.  "bar"
  242.  SF-VALUE "File Name:"       "\"afile\""
  243.  SF-VALUE "Font:"       "\"Charter\""
  244.  SF-VALUE "Font size:"  "45"
  245.  SF-COLOR "Text Color:"      '(0 0 0)
  246.  SF-TOGGLE "Transparent BG?" FALSE
  247.  SF-COLOR "Background Color:"      '(255 255 255)
  248.  SF-VALUE "Buffer amount (% height of text):" "35"
  249.  SF-TOGGLE "Flatten Image?" TRUE
  250. )
  251.  
  252. (script-fu-register
  253.  "script-fu-asc-2-img-layer"
  254.  "<Image>/Script-Fu/Utils/ASCII 2 Image Layer"
  255.  "foo"
  256.  "Chris Gutteridge: cjg@ecs.soton.ac.uk"
  257.  "30th April 1998"
  258.  "Chris Gutteridge / ECS @ University of Southampton, England"
  259.  "bar"
  260.  SF-IMAGE "Image" 0
  261.  SF-DRAWABLE "Layer" 0
  262.  SF-VALUE "File Name:"       "\"afile\""
  263.  SF-VALUE "Font:"       "\"Charter\""
  264.  SF-VALUE "Font size:"  "45"
  265.  SF-COLOR "Text Color:"      '(0 0 0)
  266.  SF-TOGGLE "Flatten Image?" TRUE
  267. )
  268.